﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using Power.BLL;
using JN.Utility;
using System.Data;

namespace Power.Manager
{
    public class ProProcess
    {
        #region property
        private Com_infoBLL comBLL = new Com_infoBLL();
        #endregion
        private static ProProcess instance = null;
        private static readonly object plcLock = new object();
        public static ProProcess Instance
        {
            get
            {
                lock (plcLock)
                {
                    if (instance == null)
                    {
                        instance = new ProProcess();
                    }
                    return instance;
                }
            }
        }
        private ProProcess()
        {

        }

        public void GetComPort()
        {
            try
            {
                List<SerialPort> list = new List<SerialPort>();
                
                //get all port in this station
                string sql = " station_id='"+ SystemStatus.Instance.Station_id +"' ";
                DataTable dtRow = comBLL.GetList(sql).Tables[0];
                if (dtRow.Rows.Count <= 0) return;

                //set in commport
                for (int i = 0; i < dtRow.Rows.Count; i++)
                {
                    SerialPort ports = new SerialPort();
                    ports.PortName = dtRow.Rows[i]["com_port"].ToString();
                    ports.BaudRate = int.Parse(dtRow.Rows[i]["baud_rate"].ToString());
                    ports.DataBits = int.Parse(dtRow.Rows[i]["data_bit"].ToString());
                    switch (dtRow.Rows[i]["odd"].ToString())
                    {
                        case "Even":
                            ports.Parity = System.IO.Ports.Parity.Even;
                            break;
                        case "Mark":
                            ports.Parity = System.IO.Ports.Parity.Mark;
                            break;
                        case "None":
                            ports.Parity = System.IO.Ports.Parity.None;
                            break;
                        case "Odd":
                            ports.Parity = System.IO.Ports.Parity.Odd;
                            break;
                        default:
                            ports.Parity = System.IO.Ports.Parity.Space;
                            break;
                    }
                    switch (dtRow.Rows[i]["stop_bit"].ToString())
                    {
                        case "Two":
                            ports.StopBits = System.IO.Ports.StopBits.Two;
                            break;
                        case "None":
                            ports.StopBits = System.IO.Ports.StopBits.None;
                            break;
                        case "One":
                            ports.StopBits = System.IO.Ports.StopBits.One;
                            break;
                        default:
                            ports.StopBits = System.IO.Ports.StopBits.OnePointFive;
                            break;
                    }
                    ports.ReadTimeout = 500;
                    ports.WriteTimeout = 500;

                    list.Add(ports);
                }

                //set to read entity
                if (!Manager.MeterEntity.SetSerialPort(list))
                {
                    SystemLogger.Write(LogType.Error, "Set meter entity serial port error!", "ProProcess");
                }

            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }
    }
}
